package org.jeecg.modules.deepseek.controller.steam;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.deepseek.pojo.ChatRequest;
import org.jeecg.modules.deepseek.pojo.ChatRequestExample;
import org.jeecg.modules.deepseek.pojo.NoteData;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;

import static dev.langchain4j.store.embedding.filter.MetadataFilterBuilder.metadataKey;

@RestController
@RequestMapping("/deepseek/steam")
@Slf4j
public class StreamController {



    @Autowired
    private EmbeddingStore<TextSegment> inMemoryEmbeddingStore;

    @Autowired
    private EmbeddingModel embeddingModel;

    @PostMapping(value = "/stream-proxy" ,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @IgnoreAuth
    public Flux<String> streamProxy(@RequestBody Map<String, Object> requestBody) {

        ChatRequest chatRequest = new ChatRequest();
        ChatRequest.Message system = new ChatRequest.Message("system", "你是公司内部的AI助手，请格式化输出内容到html的<body>标签中");
        ChatRequest.Message user = new ChatRequest.Message("user", "请将一个笑话");
        chatRequest.setMessages(List.of(system, user));
        ChatRequestExample chatRequestExample = new ChatRequestExample();
        try {
            return chatRequestExample.ChatDeepSeekSteam(chatRequest);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }



    @PostMapping ("/v1/add")
    @IgnoreAuth
    public Result<String> addDocuments(@RequestBody NoteData data) {

        TextSegment segment1 = TextSegment.from("今天买了一双鞋子", new Metadata().put("time","2025年4月").put("姓名","张昊")
                .put("鞋子","nike").put("userId",12345));
        TextSegment segment3 = TextSegment.from("今天买了一双白色鞋子", new Metadata().put("time","2025年4月").put("姓名","张昊")
                .put("鞋子","nike").put("userId",123456));
        log.info("测试数据，{}",data);
        Embedding embedding1 = embeddingModel.embed(segment1).content();
        Embedding embedding3 = embeddingModel.embed(segment3).content();
        inMemoryEmbeddingStore.add(embedding3,segment3);
     inMemoryEmbeddingStore.add(embedding1, segment1);
        Embedding queryEmbedding = embeddingModel.embed("鞋子").content();
        EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
                .queryEmbedding(queryEmbedding)
                .filter(metadataKey("userId").isEqualTo(12345))
                .maxResults(1)
                .build();

        EmbeddingSearchResult<TextSegment> relevant = inMemoryEmbeddingStore.search(embeddingSearchRequest);
        System.out.println("relevant.matches() = " + relevant.matches());
        String jsonStr = JSONUtil.toJsonStr(relevant.matches());
        return Result.OK("添加成功");
    }



}
